import jar from "./toughCookie";
const rp = require("request-promise");
import * as config from "../config";

async function emailCode() {
  const newurl = `http://192.168.30.23:5000/user/sendVerifyCode`;

  try {
    const ret = await rp({
      method: "POST",
      url: newurl,
      jar,
      body: JSON.stringify({
        index: 3,
        username: "986171791@qq.com"
      })
    });

    return JSON.parse(ret).data;
  } catch (e) {}
}

async function login() {
  const newurl = `http://192.168.30.23:5000/user/login`;

  const code = await emailCode();

  try {
    const ret = await rp({
      method: "POST",
      url: newurl,
      jar,
      body: JSON.stringify({
        code,
        password: "abc",
        username: "986171791@qq.com"
      })
    });
    console.log("login success", ret);
  } catch (e) {}
}

async function order(type: string, price: string, amount: string) {
  const newurl = `http://192.168.30.23:5000/order/takeOrder`;
  const ret = await rp({
    method: "POST",
    url: newurl,
    jar,
    body: JSON.stringify({
      amount: Number(amount),
      baseId: 2,
      counterId: 1,
      price: Number(price),
      side: type === "sell" ? 0 : 1, // 0 sell  | 1 buy
      userId: 1
    })
  });
  console.log("order success", ret);
}

async function symbol() {
  const getLoveLists = (symbolLists: any) => {
    const muLists = symbolLists.slice();
    let ret: Array<any> = [];
    for (const key in muLists) {
      if (muLists.hasOwnProperty(key)) {
        const item = muLists[key];
        ret = ret.concat(
          item.lists
            .map((d: any) => {
              return {
                ...d,
                type: "love",
                symbol: `${d.symbol}_${item.symbol}`
              };
            })
            .filter((d: any) => {
              return d.isLove;
            })
        );
      }
    }
    return ret;
  };
  const newurl = `https://api.szzc.io/front/tickers/1`;
  let start = new Date().getTime();
  console.time("start count");
  const ret = await rp({
    method: "GET",
    url: newurl,
    jar
  });

  const jsonRet = JSON.parse(ret);

  if (jsonRet && jsonRet.data) {
    const ret = [];
    for (let key in jsonRet.data) {
      const item = jsonRet.data[key];
      ret.push({
        symbol: key,
        lists: item.slice().map((d: any) => {
          return {
            h: d.high24,
            l: d.low24,
            lastPrice: d.last,
            change: d.rate,
            symbol: d.market,
            v: d.vol24,
            sort: d.sort,
            isLove: d.collect
          };
        })
      });
    }
    ret.push({
      symbol: "love",
      lists: getLoveLists(ret)
    });
    jsonRet.data = ret;
  } else {
    jsonRet.data = [];
  }
  console.log("end count", new Date().getTime() - start);
}

async function init() {
  // await symbol();
  await login();
  // await order("sell", "1.1", "1.8825");
  for (let i = 0; i < 300; i++) {
    await order("sell", "33", "1");
    console.log(i);
  }
}

init();
